package com.ebt.m.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ebt.m.widget.EmptyView;

import java.util.List;

/**
 * Created by Damon.Han on 2017/5/2 0002.
 *
 * @author Damon
 */

public abstract class SimpleAdapter<T> extends RecyclerView.Adapter<SimpleHolder> {
    protected Context mContext;
    protected int mLayout;
    protected List<T> mDatas;
    public static final int VIEW_TYPE_DATA_EMPTY = 101;
    public static final int VIEW_TYPE_DATA = 110;

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public SimpleAdapter(Context mContext, int mLayout, List<T> mDatas) {
        this.mContext = mContext;
        this.mLayout = mLayout;
        this.mDatas = mDatas;
    }

    @Override
    public SimpleHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (VIEW_TYPE_DATA_EMPTY == viewType) {
            EmptyView emptyView = new EmptyView(mContext);
            RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            emptyView.setLayoutParams(lp);
            return new SimpleHolder(emptyView);
        } else {
            View view = LayoutInflater.from(mContext).inflate(mLayout, parent, false);
            return new SimpleHolder(view);

        }
    }

    @Override
    public void onBindViewHolder(SimpleHolder holder, int position) {
        switch (getItemViewType(position)) {
            case VIEW_TYPE_DATA:
                holder.getItemView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mOnItemClickListener != null) {
                            int layoutPosition = holder.getLayoutPosition();
                            mOnItemClickListener.onItemClick(holder.getItemView(), layoutPosition);
                        }
                    }
                });
                onBindView(holder, position);
                break;
            case VIEW_TYPE_DATA_EMPTY:
                EmptyView emptyView = (EmptyView) holder.itemView;
                EmptyData emptyData = (EmptyData) mDatas.get(position);
                emptyView.setData(emptyData.name, emptyData.imgRes);
                break;
            default:
                break;
        }
    }

    //获得项的类型
    @Override
    public int getItemViewType(int position) {
        if (mDatas.get(position) instanceof EmptyData) {
            return VIEW_TYPE_DATA_EMPTY;
        } else {
            return VIEW_TYPE_DATA;
        }
    }

    public abstract void onBindView(SimpleHolder holder, int position);

    @Override
    public int getItemCount() {
        return mDatas == null ? 0 : mDatas.size();
    }

    public void addData(int pos, T t) {
        mDatas.add(pos, t);
        notifyItemInserted(pos);
    }

    public void addData(T t) {
        mDatas.add(t);
        notifyDataSetChanged();
    }

    public void removeData(int pos) {
        mDatas.remove(pos);
        notifyItemRemoved(pos);
    }
}
